const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { merge } = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const common = require('./webpack.common');
const plugins = require('./lib/plugins');
const config = require('./lib/config');

const { APP_ENV } = config;

const isAnaly = APP_ENV === 'analy';
module.exports = merge(common, {
  mode: 'production',
  plugins: [
    new CleanWebpackPlugin(),
    ...plugins,
    new CopyWebpackPlugin({
      patterns: [{ from: path.resolve(__dirname, '../public/px2rem-hike.js') },{ from: path.resolve(__dirname, '../public/favicon.ico') }],
    }),
    new MiniCssExtractPlugin({
      filename: 'style/[name].[contenthash:8].css',
    }),
    isAnaly && new BundleAnalyzerPlugin(),
  ].filter(Boolean),
});
